<?php

namespace App\Controller;

use App\Library\System\Model\AppModel;
use App\Library\System\Model\IndexModel;
use App\Library\System\Model\UserModel;
use App\Library\System\Sub\Auth;

/**
 * Index Controller
 *
 * @category   App
 * @package    App\Controller
 * @author     Loi Nguyen <loint@peslook.com>
 * @copyright  Peslook
 * @version    1.0
 * @since      Class available since Release 1.0
 */
class IndexController extends BaseController
{

    /**
     * Authentication
     *
     * @var Auth
     */
    public $auth;

    /**
     * App model
     *
     * @var AppModel
     */
    public $app;

    /**
     * Constructor
     *
     */
    public function __construct()
    {
        $this->auth = new Auth();

        if (!$this->auth->login) {

            $this->_less = array(
                'app_header',
                'welcome_login_form',
                'welcome_introduce',
                'app_footer',
                'welcome_register',
                'app_languages'
            );

            $this->_coffee = array(
                'app',
                'language'
            );

            $this->_layout = 'welcome';

        } else {

            $this->_less = array(
                #'app_search',
                'index_header',
                'index_content'
            );

            $this->_coffee = array(
                'lib/typehead',
                'lib/underscore',
                'lib/search',
                'index',
            );

            $this->_layout = 'index';

        }

        $this->app = AppModel::getInstance();
        $this->index = IndexModel::getInstance();
    }

    /**
     * Index actions
     *
     * @router /
     * @return array|ViewModel
     */
    public function indexAction()
    {
        $introduce = $this->route('introduce');

        if ($introduce) {
            $this->_less[] = 'app_introduce';
            $this->_coffee[] = 'lib/introduce';
            $this->_coffee[] = 'introduce';
        }

        $this->setLayout($this->_layout, $this->_less, $this->_coffee);

        // Create default page for root domain
        $page = $this->createPage('index');

        // Check login to devide 2 template login or welcome
        $page->login = $this->auth->login;

        // If you login, it's main page else welcome screen
        $this->auth->login ? $this->index($page) : $this->welcome($page);

        $this->support($page);

        return $page;
    }

    /**
     * Homepage if login
     *
     * @router /
     *
     * @param $page
     */
    public function index($page)
    {
        $this->func($page);
        $view = $this->getContentView($page);
        $page->user = new UserModel($this->auth->currentUser());
        $view->user = $page->user;
    }

    /**
     * Welcome page if not login
     *
     * @router /
     *
     * @param $page
     */
    public function welcome($page)
    {
        $this->setCache(array(
            'lang' => $_SESSION['app_lang'],
            'path' => '/',
            'uid'  => 0,
        ));

        $this->lang('register_username_notice', 'Your email or nickname shoud');
        $this->lang('register_password_notice', 'Your password should be at least 6 characters');
        $this->lang('register_password_notmatch', 'Your password is not match');
    }

}
